{{$path := split .Dir "/"}}
<!-- 当前是哪本书 -->
{{$book := index $path 0}}

<!-- 定义主版本，当前版本(从url中取出版本号)，版本列表 -->
{{$curVersion := index $path 1}}
{{$mainVersion := ""}}
{{$allVersion := slice ""}}

{{ if (fileExists "manual_history/version.json") }}
  {{$version := getJSON "content/manual_history/version.json"}}
  {{$mainVersion = $version.main}}
  {{$allVersion = slice $mainVersion | append (index $version "list")}}
{{end}}

{{if eq $book "manual"}}
  {{$curVersion = $mainVersion}}
{{end}}

<!-- 挂到全局，切换版本时使用 -->
<script>
  window.curBook = '{{$book}}'
  window.mainVersion = '{{$mainVersion}}'
  window.curVersion = '{{$curVersion}}'
</script>

{{if in (slice "manual" "manual_history") $book}}
  {{ template "book-menu-select-version" (dict "CurVersion" $curVersion "AllVersion" $allVersion) }}
{{end}}

{{if eq $book "manual_history"}}
  {{ with .Site.GetPage (path.Join $book $curVersion ) }}
    {{ template "book-section-children" (dict "Section" . "CurrentPage" $ "IsFirstLevel" true "ShowChild" true) }}
  {{end}}
{{else}}
  {{ with .Site.GetPage .Section }}
    {{ template "book-section-children" (dict "Section" . "CurrentPage" $ "IsFirstLevel" true "ShowChild" true) }}
  {{ end }}
{{end}}

{{ define "book-menu-select-version" }}
  {{$version := .CurVersion}}
  <div class="book-menu-select-version">
    <span class="text">ONES 版本</span>
    <div class="select-version">
      <button class="select-btn">
        <span id="current-version">{{$version}}</span>
        <svg class="arrow" height="1em" width="1em" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke="currentcolor" class="icon-7f6730be--text-3f89f380--pageDocumentIconExpanded-014d853c"><g><polyline points="9 18 15 12 9 6"></polyline></g></svg>
        
      </button>
      <div class="select-options">
        <ul>
          {{ range $v := .AllVersion }}
            <li data-val="{{$v}}" class="{{ if eq $v $version}} selected {{ end }}">
              {{$v}}
              {{- if eq $v $version}} 
                <img src='{{ "svg/selected.svg" | relURL }}' class="selected" alt="selected" />
              {{ end }}
            </li>
          {{ end }}
        </ul>
      </div>
    </div>
  </div>
{{ end }}

{{ define "book-section-children" }}{{/* (dict "Section" .Section "CurrentPage" .CurrentPage) */}}
  {{ $pages := where .Section.Pages "Params.bookhidden" "ne" true}}
  {{ if and .ShowChild $pages}}
  <ul {{ if not .IsFirstLevel }}class="book-menu-children"{{ end }}>
    {{ if and .IsFirstLevel .Section.Content }}
      <li class="book-section-flat">
        {{ template "book-menu-link" (dict "Page" .Section "CurrentPage" $.CurrentPage) }}
      </li>
    {{ end }}
    {{ range $pages }}
      {{ if .IsSection }}
        <li {{ if and $.IsFirstLevel .Params.BookFlatSection }} class="book-section-flat" {{ end }}>
          {{ if and $.IsFirstLevel .Params.BookFlatSection }} 
            {{ template "book-menu-group" (dict "Page" . "CurrentPage" $.CurrentPage) }}
          {{ else }}
            {{ template "book-menu-link" (dict "Page" . "CurrentPage" $.CurrentPage) }}
          {{ end }}
          {{ $showChild := false}}
          {{ if $.IsFirstLevel}}
            {{ $showChild = true}}
          {{ else if and .IsSection (eq $.CurrentPage .)}}
            {{ $showChild = true}}  
          {{ else if and .IsSection (.IsAncestor $.CurrentPage)}}
            {{ $showChild = true}}  
          {{ end }}
          {{ template "book-section-children" (dict "Section" . "CurrentPage" $.CurrentPage "IsFirstLevel" false "ShowChild" $showChild) }}
        </li>
      {{ else if and .IsPage .Content}}
        <li>
          {{ template "book-menu-link" (dict "Page" . "CurrentPage" $.CurrentPage) }}
        </li>
      {{ end }}
    {{ end }}
  </ul>
  {{ end }}
{{ end }}

{{ define "book-menu-group" }}
  <div class="book-menu-group-item">
    <a {{if .Page.Content}}href="{{ .Page.RelPermalink }}"{{end}} class="
      book-menu-group-item-inner 
      {{ if .Page.Content}} hascontent {{ end }}
      {{ if eq .CurrentPage .Page}} active {{ end }}
    ">
      <span class="book-menu-group-item-text">{{- partial "docs/title" .Page -}}</span>
    </a>
  </div>
{{ end }}


{{ define "book-menu-link" }}{{/* (dict "Page" .Page "CurrentPage" .CurrentPage) */}}
  <div class="book-menu-item">
    <a href="{{ .Page.RelPermalink }}" class="
      book-menu-item-inner 
      {{- if eq .CurrentPage .Page}} active {{ end }}
      {{- if and .Page.IsSection (not .Page.Params.BookFlatSection) .Page.Pages }} collapsed {{ end }}
      {{ if and .Page.IsSection (.Page.IsAncestor $.CurrentPage) }} collapsed-open {{ end }}
    ">
    <span class="book-menu-item-text">{{- partial "docs/title" .Page -}}</span>
    {{- if and .Page.IsSection (not .Page.Params.BookFlatSection) .Page.Pages }}
    <span class="book-menu-item-icon">
      {{ partial "svgs/arrow.svg" }}
    </span>
    {{ end }}
    </a>
  </div>
{{ end }}
